package com.leyou.item.controller;

import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //新增商品信息
    @PostMapping("/goods")
    public ResponseEntity<Void> goodsSave(@RequestBody SpuDTO spuDTO){
        goodsService.goodsSave(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改商品上下架
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam("id")Long spuId, @RequestParam("saleable")Boolean saleable){
        goodsService.updateSaleable(spuId,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //回显商品spuDetail数据
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id")Long spuId){
        return ResponseEntity.ok(goodsService.querySpuDetail(spuId));
    }

    //回显商品sku数据
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySku(@RequestParam("id")Long spuId){
        return ResponseEntity.ok(goodsService.querySku(spuId));
    }

    //修改商品
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //删除商品信息
    @GetMapping("del/spu/detail")
    public ResponseEntity<Void> deleteGoods(@RequestParam("id")Long spuId){
        goodsService.deleteGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据id查询spu
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }
}
